﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;

namespace AnimeWatchMax
{
    public class UIManager : FrameWork.WinUI.Controls.UIManagerBase
    {
        public DataObjects.Data Data { get; private set; }
        public frmTitleList TitleListPanel { get; private set; }
        public frmGroupList GroupListPanel { get; private set; }
        public frmTitle TitlePanel { get; private set; }

        public UIManager(DockPanel pDockPanel, EventManager pEventManager)
            : base(pDockPanel, pEventManager)
        {

        }

        public void InitData(DataObjects.Data pData)
        {
            CloseAllTitleForms();
            this.Data = pData;
            this.TitleListPanel.UpdateViewer();
            this.GroupListPanel.UpdateViewer();
        }

        public void UpdateViewer()
        {
        }

        public void ViewTitle(DataObjects.Title pTitle)
        {
            if (this.TitlePanel == null)
            {
                this.TitlePanel = new frmTitle();
                this.TitlePanel.UIManager = this;
                this.TitlePanel.MdiParent = this.ParentForm;
                this.TitlePanel.Show(this.DockPanel);
                this.TitlePanel.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.Document;
                this.TitlePanel.DockState = DockState.Document;
                this.TitlePanel.VisibleState = DockState.Document;
            }

            this.TitlePanel.View(pTitle);
        }

        public string ShowHideTitleListPane()
        {
            if (this.TitleListPanel.Visible)
            {
                this.TitleListPanel.Hide();
            }
            else
            {
                this.TitleListPanel.Show();
            }

            return this.TitleListPanel.Visible ? "Hide Title List Pane" : "Show Title List Pane";
        }

        public string ShowHideGroupListPane()
        {
            if (this.GroupListPanel.Visible)
            {
                this.GroupListPanel.Hide();
            }
            else
            {
                this.GroupListPanel.Show();
            }

            return this.GroupListPanel.Visible ? "Hide Group List Pane" : "Show Group List Pane";
        }

        public EventManager EventManager
        {
            get
            {
                return (EventManager)base.EventManagerBase;
            }
        }

        public void InitTitleListPanel()
        {
            if (this.TitleListPanel == null)
            {
                this.TitleListPanel = new frmTitleList();
                this.TitleListPanel.UIManager = this;
                this.TitleListPanel.MdiParent = this.ParentForm;
                this.TitleListPanel.Show(this.DockPanel);
                this.TitleListPanel.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.Float | WeifenLuo.WinFormsUI.Docking.DockAreas.DockLeft | WeifenLuo.WinFormsUI.Docking.DockAreas.DockRight | WeifenLuo.WinFormsUI.Docking.DockAreas.DockTop | WeifenLuo.WinFormsUI.Docking.DockAreas.DockBottom | WeifenLuo.WinFormsUI.Docking.DockAreas.Document;
                this.TitleListPanel.DockState = DockState.DockLeft;
                this.TitleListPanel.VisibleState = DockState.DockLeft;
            }
        }

        public void InitGroupListPanel()
        {
            if (this.GroupListPanel == null)
            {
                this.GroupListPanel = new frmGroupList();
                this.GroupListPanel.UIManager = this;
                this.GroupListPanel.MdiParent = this.ParentForm;
                this.GroupListPanel.Show(this.DockPanel);
                this.GroupListPanel.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.Float | WeifenLuo.WinFormsUI.Docking.DockAreas.DockLeft | WeifenLuo.WinFormsUI.Docking.DockAreas.DockRight | WeifenLuo.WinFormsUI.Docking.DockAreas.DockTop | WeifenLuo.WinFormsUI.Docking.DockAreas.DockBottom | WeifenLuo.WinFormsUI.Docking.DockAreas.Document;
                this.GroupListPanel.DockState = DockState.DockLeft;
                this.GroupListPanel.VisibleState = DockState.DockLeft;
            }
        }

        public void CloseAllTitleForms()
        {
            foreach (DockPane pane in this.DockPanel.Panes)
            {
                foreach (DockContent content in pane.Contents)
                {
                    if ((content is frmTitle))
                    {
                        ((frmTitle)content).Close();

                    }
                }
            }
        }

        //public void CloseTitleForm(DataObjects.Title pTitle)
        //{
        //    foreach (DockPane pane in this.DockPanel.Panes)
        //    {
        //        foreach (DockContent content in pane.Contents)
        //        {
        //            if ((content is frmTitle) && (((frmTitle)content).Key == pTitle.DataObjectGUID))
        //            {
        //                ((frmTitle)content).Close();

        //                return;
        //            }
        //        }
        //    }
        //}

        //public void ViewTitle(DataObjects.Title pTitle)
        //{


        //    foreach (DockPane pane in this.DockPanel.Panes)
        //    {
        //        foreach (DockContent content in pane.Contents)
        //        {
        //            if ((content is frmTitle) )
        //            {
        //                var tForm = ((frmTitle)content);

        //                if (tForm.Key == pTitle.DataObjectGUID)
        //                {
        //                    tForm.UpdateViewer();
        //                }
        //                else
        //                {
        //                }
                      
        //                return;
        //            }
        //        }
        //    }
        //}

        ~UIManager()
        {

            CloseAllTitleForms();

            this.TitleListPanel.Close();
            this.TitleListPanel.Dispose();
            this.TitleListPanel = null;

            this.GroupListPanel.Close();
            this.GroupListPanel.Dispose();
            this.GroupListPanel = null;
        }

    }
}
